<template>
  <div class="system-pm_time_consuming-container">
    <el-dialog v-model="state.isShowDialog" width="769px" center>
      <template #title>
        <div style="font-size: large" v-drag="['.system-pm_time_consuming-container .el-dialog', '.system-pm_time_consuming-container .el-dialog__header']">{{title}}</div>
      </template>
      <el-form
        :model="state.ruleForm"
        :rules="state.ruleRules"
        ref="ruleFormRef"
        label-width="80px"
      >
      <el-form-item label="工作成果" prop="achievement">
          <el-input v-model="state.ruleForm.achievement" placeholder="请输入工作成果" />
      </el-form-item>
      <el-form-item label="客户名称" prop="customerName">
          <el-input v-model="state.ruleForm.customerName" placeholder="请输入客户名称" />
      </el-form-item>
      <el-form-item label="效率评分" prop="efficiencyScore">
          <el-input v-model="state.ruleForm.efficiencyScore" placeholder="请输入效率评分" />
      </el-form-item>
      <el-form-item label="项目名称" prop="projectName">
          <el-input v-model="state.ruleForm.projectName" placeholder="请输入项目名称" />
      </el-form-item>
      <el-form-item label="客户编码" prop="customerId">
          <el-input v-model="state.ruleForm.customerId" placeholder="请输入客户编码" />
      </el-form-item>
      <el-form-item label="项目阶段" prop="projectStage">
          <el-input v-model="state.ruleForm.projectStage" placeholder="请输入项目阶段" />
      </el-form-item>
      <el-form-item label="审核时间" prop="auditTime">
          <el-date-picker clearable style="width: 200px"
                 v-model="state.ruleForm.auditTime"
                 type="date" value-format="YYYY-MM-DD"
                 placeholder="选择审核时间">
          </el-date-picker>
      </el-form-item>
      <el-form-item label="项目经理名称" prop="staffManagerName">
          <el-input v-model="state.ruleForm.staffManagerName" placeholder="请输入项目经理名称" />
      </el-form-item>
      <el-form-item label="成本预估" prop="cost">
          <el-input v-model="state.ruleForm.cost" placeholder="请输入成本预估" />
      </el-form-item>
      <el-form-item label="项目编码" prop="projectId">
          <el-input v-model="state.ruleForm.projectId" placeholder="请输入项目编码" />
      </el-form-item>
      <el-form-item label="工作耗时" prop="timeConsuming">
          <el-input v-model="state.ruleForm.timeConsuming" placeholder="请输入工作耗时" />
      </el-form-item>
      <el-form-item label="项目经理编码" prop="staffManagerId">
          <el-input v-model="state.ruleForm.staffManagerId" placeholder="请输入项目经理编码" />
      </el-form-item>
      <el-form-item label="成果评分" prop="outcomeAssessment">
          <el-input v-model="state.ruleForm.outcomeAssessment" placeholder="请输入成果评分" />
      </el-form-item>
      
       <el-form-item label="审核状态" prop="auditStatus">
          <el-select v-model="state.ruleForm.auditStatus" placeholder="请选择审核状态">
              <el-option
                  v-for="dict in state.auditStatusOptions"
                  :key="dict.dictValue"
                  :value="Number(dict.dictValue)? Number(dict.dictValue) : dict.dictValue"
                  :label="dict.dictLabel"
              >{{ dict.dictLabel }}</el-option>
          </el-select>
       </el-form-item>
      
      <el-form-item label="员工类型" prop="staffStatus">
          <el-input v-model="state.ruleForm.staffStatus" placeholder="请输入员工类型" />
      </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="onCancel">取 消</el-button>
          <el-button type="primary" @click="onSubmit" :loading="state.loading">编 辑</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup name="editPm_time_consuming">
import { reactive, ref, unref, getCurrentInstance } from "vue";
import { get,add, update } from "@/api/public/basic";
import { ElMessage } from "element-plus";

const props = defineProps({
  title: {
    type: String,
    default: () => "",
  },
})

const { proxy } = getCurrentInstance() as any;
const ruleFormRef = ref<HTMLElement | null>(null);
const state = reactive({
  // 是否显示弹出层
  isShowDialog: false,
  loading: false,
  ruleForm: {id: undefined,achievement: undefined,customerName: undefined,efficiencyScore: undefined,deleteTime: undefined,projectName: undefined,createTime: undefined,customerId: undefined,projectStage: undefined,auditTime: undefined,staffManagerName: undefined,cost: undefined,projectId: undefined,proprietorId: undefined,updateTime: undefined,timeConsuming: undefined,staffManagerId: undefined,outcomeAssessment: undefined,auditStatus: undefined,flowId: undefined,proprietorName: undefined,staffStatus: undefined,weekFlowId: undefined,
  },
  // auditStatusOptions字典数据
  auditStatusOptions: [],
  // 表单校验
  ruleRules: {
    customerName: [
      { required: true, message: "客户名称不能为空", trigger: "blur" }
    ],
    deleteTime: [
      { required: true, message: "删除时间不能为空", trigger: "blur" }
    ],
    projectName: [
      { required: true, message: "项目名称不能为空", trigger: "blur" }
    ],
    customerId: [
      { required: true, message: "客户编码不能为空", trigger: "blur" }
    ],
    projectStage: [
      { required: true, message: "项目阶段不能为空", trigger: "blur" }
    ],
    staffManagerName: [
      { required: true, message: "项目经理名称不能为空", trigger: "blur" }
    ],
    projectId: [
      { required: true, message: "项目编码不能为空", trigger: "blur" }
    ],
    staffManagerId: [
      { required: true, message: "项目经理编码不能为空", trigger: "blur" }
    ],
  },
});
// 打开弹窗
const openDialog = (row: any) => {
  get(row.flowId, 'hS9i1PUcWXs').then((response) => {
    state.ruleForm = response.data?.length ? response.data[0] : {};
  });

  state.isShowDialog = true;
  state.loading = false;
 proxy.getDicts("oa_event_process").then((response: any) => {
     state.auditStatusOptions = response.data;
 });
}

// 关闭弹窗
const closeDialog = (row?: object) => {
  proxy.mittBus.emit("onEditPm_time_consumingModule", row);
  state.isShowDialog = false;
};
// 取消
const onCancel = () => {
  closeDialog();
};

// 保存
const onSubmit = () => {
  const formWrap = unref(ruleFormRef) as any;
  if (!formWrap) return;
  formWrap.validate((valid: boolean) => {
    if (valid) {
      state.loading = true;
      if (state.ruleForm?.flowId) {
        update(state.ruleForm.flowId, state.ruleForm, 'hS9i1PUcWXs').then((response) => {
          ElMessage.success('修改成功');
          state.loading = false;
          closeDialog(state.ruleForm); // 关闭弹窗
        });
      } else {
        add(state.ruleForm, 'hS9i1PUcWXs').then((response) => {
          ElMessage.success('新增成功');
          closeDialog(state.ruleForm); // 关闭弹窗
        });
      }
    }
  });
};

defineExpose({
  openDialog,
});
</script>